package mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import mybatis.entity.User;
import mybatis.mapper.UserMapper;
import mybatis.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-09-20 11:34
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    // @Transactional
    @Override
    public void register(User user) throws ClassNotFoundException {
        userMapper.insert(user);

        // System.out.println(5/0);
        Class.forName("aaaa");
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, timeout = 6)
    @Override
    public User login(String username, String password) {
        User user = userMapper.selectByUsernameAndPassword(username, password);
        return user;
    }

    @Override
    public PageInfo<User> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
}
